package com.yy.dao.impl;

import com.yy.dao.IResumeDAO;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.yy.model.TbCompany;
import com.yy.model.TbResume;
import com.yy.model.TbJob;

@Component


public class ResumeDAO implements IResumeDAO{
	@Resource
	private SessionFactory SessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public int getCompanyId(String companyLoginName) throws Exception {
		int result=0;
		try{
			
			String hal="select companyId from TbCompany where companyManName='"+companyLoginName+"'";
			System.out.println("this JU DAO **** 11");
			Query query=this.SessionFactory.getCurrentSession().createQuery(hal);
			result=Integer.parseInt(query.uniqueResult().toString());
			System.out.println("this JU DAO **** 11");
		}catch(Exception e){
			e.printStackTrace();
			result=0;
		}
		return result;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getJobId(Integer companyId) throws Exception {
		// TODO Auto-generated method stub
		System.out.print("this JU DAO **** 22222222");
		Session session=SessionFactory.getCurrentSession();	
		String hql="select from jobId from TbJob where jobCompany="+companyId+"";
		Query query=session.createQuery(hql);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TbResume> getAllResume(int jobId) throws Exception {
		// TODO Auto-generated method stub
		
		Session session=SessionFactory.getCurrentSession();	
		String hql="select tbresume from TbResume as tbresume where jobId="+jobId+"";
		System.out.print("this JU DAO **** 3333333333");
		Query 	query=session.createQuery(hql);

		return query.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public int getResumeCount(int jobId) throws Exception {
		// TODO Auto-generated method stub
		int results = 0; 
		try{
			System.out.println("enter into Resume Count ceng*************");
			Session session=SessionFactory.getCurrentSession();
			String hql="select count(*) from TbResume where jobId="+jobId+"";
			Query query=session.createQuery(hql);
			results=Integer.parseInt(query.uniqueResult().toString());
			System.out.println("finish Reusme select ****************");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return results;
	}
	
	@Override
	public Integer addResume(TbResume resumeBean) throws Exception{
		Integer result = 0;
		try{
			Session session = SessionFactory.getCurrentSession();
			result = (Integer) session.save(resumeBean);
		}catch(Exception e){
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
	@Override
	public Integer addCommand(int resumeId, String resumeState)
			throws Exception {
		Integer result=0;
		try{
			System.out.println("Start command DAO ***************");
			Session session=SessionFactory.getCurrentSession();
			String hql="update TbResume set resumeState='"+resumeState+"' where resumeId="+resumeId+"";
			Query query=session.createQuery(hql);
			query.executeUpdate();
			result=10;
			System.out.println("end command DAO ***************");
		}catch(Exception e){
			e.printStackTrace();
			result=0;
		}
		
		
		// TODO Auto-generated method stub
		return result;
	}
	
	@Override
	public List<TbResume> getResumuByName(String loginName)throws Exception{
		Session session=SessionFactory.getCurrentSession();
		String hql="select tbresume from TbResume as tbresume where resumeName=:userName";
		System.out.print("this JU DAO **** 3333333333");
		Query 	query=session.createQuery(hql);
		query.setString("userName", loginName);
		return query.list();
	}
	
	@Override
	public int getNumByName(String loginName) throws Exception{
		int result = 0; 
		try{
			System.out.println("enter into Resume Count ceng*************");
			Session session=SessionFactory.getCurrentSession();
			String hql="select count(*) from TbResume where resumeName=:userName";
			Query query=session.createQuery(hql);
			query.setString("userName", loginName);
			result=Integer.parseInt(query.uniqueResult().toString());
			System.out.println("finish Reusme select ****************");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Override
	public int delResumeById(int resumeId) throws Exception{
		int ret = -1;
		Session session=SessionFactory.getCurrentSession();
		String hql = "delete from  TbResume  where resumeId = " + resumeId;
		Query queryupdate = session.createQuery(hql);
		ret= queryupdate.executeUpdate();
		return  ret;
	}

}
